/*
 * @Author: AskMeWhy
 * @Date: 2020-12-03 10:01:55
 * @Last Modified by: AskMeWhy
 * @Last Modified time: 2021-07-27 17:49:28
 */
import { store } from "@/store";
export class AuthServer {
    get authList(): any {
        const user: any = store.getters.getUser;
        return user.auth.list || {};
        // return {
        //     home: {
        //         add: true,
        //         remove: true,
        //         edit: true
        //     },
        //     home1: {
        //         add: false,
        //         remove: false,
        //         edit: true
        //     },
        //     home2: true
        // };
    }

    /**
     *
     * @param payload
     * {page1: [], page2: []} ==> authList: {page1:true,page2:{a: true,b:true}}
     * {page1: [["add"],["remove"]], page2: []} ==> authList: {page1: {add: true},page2:{a: true,b:true}}
     * {page1: [["add","o","p"],["remove"]], page2: []} ==> authList: {page1: {add: {o:true}},page2:{a: true,b:true}}
     */
    checkAuth(payload: any) {
        // 所有未在authList存在的配置项都不具有权限，都是false来处理
        // console.log(payload, this.authList);
        let res = false;
        for (const key in payload) {
            const curAuth = this.authList[key] || false;
            const subPayload = payload[key];
            if (typeof curAuth === "boolean") {
                res = curAuth;
                if (res) {
                    break;
                } else {
                    continue;
                }
            }
            // 如果subPayload的长度大于0 则需要判断数组每一项是否存在与curAuth
            // 是且为false则代表没有权限
            // 其他情况则代表有权限
            if (subPayload.length > 0) {
                for (const subKey of subPayload) {
                    res = this.checkArrHasAuth(subKey, curAuth);
                    if (res) {
                        continue;
                    } else {
                        break;
                    }
                }
            // 如果subPayload的长度为0 则需要判断curAuth自己所有项
            // 所有项有一项为false则代表没有权限
            } else {
                res = this.checkObjHasAuth(curAuth);
                // console.log("res", res);
                if (res) {
                    break;
                } else {
                    continue;
                }
            }
            if (res) {
                break;
            }
        }
        return res;
    }

    private checkObjHasAuth(obj: any) {
        let res = true;
        for (const key in obj) {
            if (typeof obj[key] === "boolean") {
                res = obj[key];
                break;
            }
            if (!this.checkObjHasAuth(obj[key])) {
                res = false;
                break;
            }
        }
        return res;
    }

    private checkArrHasAuth(arrList: any[], authObj: any) {
        let res = true;
        if (arrList.length === 0) {
            return res;
        }
        const cur = authObj[arrList[0]] || false;
        if (typeof cur === "boolean") {
            res = cur;
            return res;
        }
        if (!this.checkArrHasAuth(arrList.slice(1), cur)) {
            res = false;
            return res;
        }
        return res;
    }
}
